package cn.bocweb.spinning.view.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.Space;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;

import butterknife.Bind;
import butterknife.ButterKnife;
import cn.bocweb.spinning.R;
import cn.bocweb.spinning.model.Bean.FinishExamBean;
import cn.bocweb.spinning.model.Bean.FirstMatchBean;
import cn.bocweb.spinning.model.Bean.FirstRewardBean;
import cn.bocweb.spinning.model.Bean.LibListBean;
import cn.bocweb.spinning.model.Bean.MatchShareBean;
import cn.bocweb.spinning.model.Bean.SecondTestBean;
import cn.bocweb.spinning.model.Bean.UserBean;
import cn.bocweb.spinning.presenter.Imp.UserPresenterImp;
import cn.bocweb.spinning.presenter.UserPresenter;
import cn.bocweb.spinning.util.Constant;
import cn.bocweb.spinning.util.DeviceUtil;
import cn.bocweb.spinning.util.WXUtil;
import cn.bocweb.spinning.view.common.BaseActivity;
import cn.bocweb.spinning.view.common.GlideCircle;
import cn.bocweb.spinning.view.dialog.TestResultDialog;
import de.greenrobot.event.EventBus;

public class TestResultActivity extends BaseActivity implements View.OnClickListener {

    TestResultDialog testResultDialog;
    String testid, libid;
    boolean isFirstMatch, isSecondTest;
    UserPresenter mUserPresenter;
    int friend_1_timeline_2;
    String count;

    @Bind(R.id.btn_exit)
    ImageView btnExit;
    @Bind(R.id.btn_check_answer)
    TextView btnCheckAnswer;
    @Bind(R.id.btn_check_wrong)
    TextView btnCheckWrong;
    @Bind(R.id.btn_rank)
    TextView btnRank;
    @Bind(R.id.exam_result_level)
    TextView examResultLevel;
    @Bind(R.id.exam_result_icon)
    ImageView examResultIcon;
    @Bind(R.id.exam_result_nickname)
    TextView examResultNickname;
    @Bind(R.id.exam_result_point)
    TextView examResultPoint;
    @Bind(R.id.exam_result_time_spend)
    TextView examResultTimeSpend;
    @Bind(R.id.exam_result_point_level)
    TextView examResultPointLevel;
    @Bind(R.id.exam_result_money)
    TextView examResultMoney;
    @Bind(R.id.exam_result_money_level)
    TextView examResultMoneyLevel;

    @Bind(R.id.test_result_share_friend)
    LinearLayout testResultShareFriend;
    @Bind(R.id.test_result_share_timeline)
    LinearLayout testResultShareTimeline;
    @Bind(R.id.test_result_rule)
    TextView mTestResultRule;
    @Bind(R.id.btn_reward)
    TextView mBtnReward;
    @Bind(R.id.btn_repeat)
    TextView mBtnRepeat;
    @Bind(R.id.textview_score)
    TextView textviewScore;
    @Bind(R.id.space1)
    Space space1;
    @Bind(R.id.space0)
    Space space0;
    @Bind(R.id.space2)
    Space space2;
    @Bind(R.id.space3)
    Space space3;
    @Bind(R.id.textview_title)
    TextView textviewTitle;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test_result);

        mUserPresenter = new UserPresenterImp();
        EventBus.getDefault().register(this);

        dialog.show();

        ButterKnife.bind(this);
        initDialog();
        initIntent();
        initClickBtn();
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this);
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
        if (EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().unregister(this);
        }
    }

    @Override
    protected void onStop() {
        super.onStop();

    }

    public void onEventMainThread(FirstMatchBean bean) {
        if (bean.getReturn_code() == 0) {
            Intent intent = new Intent(this, ReadyTestActivity.class);
            intent.putExtra(Constant.TO_READY_FIRST_TEST_ID, bean.getId());
            intent.putExtra(Constant.TO_TEST_RANK_IF_FIRST, true);
            startActivity(intent);
            finish();
        }
    }

    public void onEventMainThread(FirstRewardBean bean) {
        if (bean.getReturn_code() == 0) {
            Intent intent = new Intent(this, FirstRewardActivity.class);
            intent.putExtra(Constant.TO_FIRST_REWARD, bean.getUrl());
            startActivity(intent);
        } else {
            DeviceUtil.MyToast(this, bean.getMsg());
        }
    }

    public void onEventMainThread(MatchShareBean bean) {
        if (bean.getReturn_code() == 0) {
            if (friend_1_timeline_2 == 1) {
                if (isFirstMatch) {
                    WXUtil.WXShareToFriend(bean.getUrl(), this, "纺织大学堂", "我参加了梳理知识大奖赛，快来看看我考了第几名吧!");
                } else if (isSecondTest) {
                    WXUtil.WXShareToFriend(bean.getUrl(), this, "纺织大学堂", "我参加了梳理知识大奖赛，快来看看我考了第几名吧!");
                } else {
                    WXUtil.WXShareToFriend(bean.getUrl(), this);
                }
            } else {
                if (isFirstMatch) {
                    WXUtil.WXShareToTimeline(bean.getUrl(), this, "纺织大学堂", "我参加了梳理知识大奖赛，快来看看我考了第几名吧!");
                } else if (isSecondTest) {
                    WXUtil.WXShareToTimeline(bean.getUrl(), this, "纺织大学堂", "我参加了梳理知识大奖赛，快来看看我考了第几名吧!");
                } else {
                    WXUtil.WXShareToTimeline(bean.getUrl(), this);
                }
            }
        }
    }


    private void initIntent() {
        Intent intent = getIntent();
        FinishExamBean.DataEntity dataEntity = (FinishExamBean.DataEntity) intent.getSerializableExtra(Constant.EXAM_RESULT);
        space1.setVisibility(View.GONE);

        isSecondTest = intent.getBooleanExtra(Constant.EXAM_RESULT_ISSECONDTEST, false);
        libid = intent.getStringExtra(Constant.EXAM_RESULT_LIBID);
        isFirstMatch = intent.getBooleanExtra(Constant.EXAM_RESULT_ISFIRSTMATCH, false);
        testid = dataEntity.getTestid();
        examResultLevel.setText(dataEntity.getTitle());
        String icon = dataEntity.getUthumb();
        String nickname = dataEntity.getNickname();
        if (icon == null || icon.isEmpty()) {
            examResultIcon.setImageResource(R.mipmap.drawer_icon_unlogin);
        } else {
            Glide.with(this).load(icon).transform(new GlideCircle(this)).into(examResultIcon);
        }
        if (nickname == null || nickname.isEmpty()) {
            examResultNickname.setText(Constant.NICKNAME_UNLOGIN);
        } else {
            examResultNickname.setText(nickname);
        }
        examResultPoint.setText(String.valueOf(dataEntity.getScore()));
        examResultPointLevel.setText(dataEntity.getSubtitle());
        String min = String.valueOf(Integer.parseInt(dataEntity.getSpend_time()) / 60);
        String sec = String.valueOf(Integer.parseInt(dataEntity.getSpend_time()) % 60);
        examResultTimeSpend.setText(min + "分" + sec + "秒");
        examResultMoney.setText(dataEntity.getScope());
        examResultMoneyLevel.setText(dataEntity.getScopeintro());
        if (isFirstMatch) {
            textviewTitle.setVisibility(View.VISIBLE);
            btnCheckAnswer.setVisibility(View.GONE);
            btnCheckWrong.setVisibility(View.GONE);
            btnRank.setText("初赛排行榜");
            textviewScore.setText("初赛分数:");
            mTestResultRule.setVisibility(View.VISIBLE);
            if (dataEntity.getScore() > 80) {
                mBtnReward.setVisibility(View.VISIBLE);
                mBtnRepeat.setVisibility(View.GONE);
                space1.setVisibility(View.VISIBLE);
            } else {

                space3.setVisibility(View.VISIBLE);
                space1.setVisibility(View.VISIBLE);
                mBtnReward.setVisibility(View.GONE);
                mBtnRepeat.setVisibility(View.VISIBLE);
            }
        } else if (isSecondTest) {
//            textviewScore.setText("复赛分数:");
//            btnRank.setText("复赛排行榜");

            textviewScore.setText("决赛分数:");
            btnRank.setText("决赛排行榜");

            textviewTitle.setVisibility(View.VISIBLE);
            space2.setVisibility(View.VISIBLE);
            btnCheckAnswer.setVisibility(View.GONE);
            btnCheckWrong.setVisibility(View.GONE);
            mTestResultRule.setVisibility(View.GONE);
            mBtnRepeat.setVisibility(View.GONE);
            if (dataEntity.getScore() > 80) {
                mBtnReward.setVisibility(View.GONE);
            }
        } else {
            LibListBean.DataEntity dataEntity1 = new LibListBean.DataEntity();
            dataEntity1.readLibListIntoLocal(this);
            textviewScore.setText(dataEntity1.getTitle() + "分数:");
            mTestResultRule.setVisibility(View.GONE);
            space0.setVisibility(View.VISIBLE);
            space1.setVisibility(View.VISIBLE);
        }

        if (dialog.isShowing()) {
            dialog.dismiss();
        }
    }

    private void initDialog() {
        testResultDialog = new TestResultDialog(this);
    }

    private void initClickBtn() {
        btnRank.setOnClickListener(this);
        btnCheckAnswer.setOnClickListener(this);
        btnCheckWrong.setOnClickListener(this);
        btnExit.setOnClickListener(this);
        testResultShareFriend.setOnClickListener(this);
        testResultShareTimeline.setOnClickListener(this);
        mBtnRepeat.setOnClickListener(this);
        mBtnReward.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        Intent intent;
        switch (v.getId()) {
            case R.id.btn_rank: {
                intent = new Intent(TestResultActivity.this, RankActivity.class);
                intent.putExtra(Constant.TO_RANK_LIBID, libid);
                intent.putExtra(Constant.TO_TEST_RANK_IF_FIRST, isFirstMatch);
                intent.putExtra(Constant.TO_TEST_RANK_IF_SECOND, isSecondTest);
                startActivity(intent);
                break;
            }
            case R.id.btn_check_answer: {
                intent = new Intent(TestResultActivity.this, CheckAnswerActivity.class);
                intent.putExtra(Constant.CHECK_ANSWER_TESTID, testid);
                startActivity(intent);
                break;
            }
            case R.id.btn_check_wrong: {
                intent = new Intent(TestResultActivity.this, CheckAnswerActivity.class);
                intent.putExtra(Constant.CHECK_ERROR_TESTID, testid);
                startActivity(intent);
                break;
            }
            case R.id.btn_exit: {
                finish();
                break;
            }
            case R.id.test_result_share_friend: {
                friend_1_timeline_2 = 1;
                UserBean.DataEntity dataEntity = new UserBean.DataEntity();
                dataEntity.readInfoFromLocal(this);
                mUserPresenter.matchShare(dataEntity.getUserid(), testid);
                break;
            }
            case R.id.test_result_share_timeline: {
                friend_1_timeline_2 = 2;
                UserBean.DataEntity dataEntity = new UserBean.DataEntity();
                dataEntity.readInfoFromLocal(this);
                mUserPresenter.matchShare(dataEntity.getUserid(), testid);
                break;
            }
            case R.id.btn_reward: {
                UserBean.DataEntity dataEntity = new UserBean.DataEntity();
                dataEntity.readInfoFromLocal(this);
                mUserPresenter.getFirstReward(dataEntity.getUserid());
                break;
            }
            case R.id.btn_repeat: {
                if (!isSecondTest) {
                    mUserPresenter.getFirstMatch();
                } else {
                    UserBean.DataEntity dataEntity = new UserBean.DataEntity();
                    dataEntity.readInfoFromLocal(this);
                    mUserPresenter.secondTest(dataEntity.getUserid());
                }
                break;
            }
        }
    }

    public void onEventMainThread(SecondTestBean bean) {
        DeviceUtil.Log("bean = " + bean.getMsg());
        if (bean.getReturn_code() == 0) {
            Intent intent = new Intent(this, ReadyTestActivity.class);
            intent.putExtra(Constant.TO_READY_FIRST_TEST_ID, String.valueOf(bean.getId()));
            intent.putExtra(Constant.TO_SECOND_RULE_IS_SECOND_TEST, true);
            startActivity(intent);
            finish();
        }
    }
}
